package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    // 获取秒杀商品列表
    @GetMapping("seckill.html")
    public String index(Model model){
        Result result = activityFeignClient.findAll();
        model.addAttribute("list",result.getData());
        return "seckill/index";
    }

    // 获取秒杀商品详情
    @GetMapping("seckill/{skuId}.html")
    public String getItem(@PathVariable Long skuId,Model model){
        // 需要存储一个item
        Result result = activityFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item",result.getData());
        return "seckill/item";
    }

    //http://activity.gmall.com/seckill/queue.html?skuId=46&skuIdStr=c81e728d9d4c2f636f067f89cc14862c
    @GetMapping("seckill/queue.html")
    public String queue(Long skuId, String skuIdStr, HttpServletRequest request){
        request.setAttribute("skuId",skuId);
        request.setAttribute("skuIdStr",skuIdStr);

        return "seckill/queue";
    }

    // 去下单页面
    @GetMapping("seckill/trade.html")
    public String trade(Model model){
        // 当前需要在后台存储detailArrayList，userAddressList totalNum totalAmount
        // 调用秒杀活动模块的方法
        Result<Map<String,Object>> result = activityFeignClient.trade();
        if(result.isOk()){
            // 保存秒杀订单需要的数据
            model.addAllAttributes(result.getData());
            // 返回下单成功页面
            return "seckill/trade";
        }else {
            model.addAttribute("message","下单失败");
            // 下单失败，返回失败界面
            return "seckill/fail";
        }

    }
}
